package com.ossjk.oa.advisory.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.oa.advisory.entity.Project;
import com.ossjk.oa.advisory.entity.Projectgroup;
import com.ossjk.oa.advisory.entity.Subproject;
import com.ossjk.oa.advisory.mapper.ProjectMapper;
import com.ossjk.oa.advisory.service.IProjectService;
import com.ossjk.oa.advisory.service.IProjectgroupService;
import com.ossjk.oa.advisory.service.ISubprojectService;

/**
 * <p>
 * 项目 服务实现类
 * </p>
 *
 * @author Jason
 * @since 2019-02-11
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
	@Autowired
	private IProjectService iProjectService;
	@Autowired
	private ISubprojectService iSubprojectService;
	@Autowired
	private IProjectgroupService iProjectgroupService;

	@Override
	public Page selectVoPage(Page page, Wrapper wrapper) {
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(baseMapper.selectVoPage(page, wrapper));
		return page;
	}

	@Override
	public boolean updateDetail(Project project, String[] spname, String[] did, String[] syears, String[] sremarks, String[] pgeid, String[] pgpid, String[] pgtype, String[] pgremarks) {
		// TODO Auto-generated method stub
		if (!CommonUtil.isBlank(spname) && !CommonUtil.isBlank(did) && !CommonUtil.isBlank(syears)) {
			if (spname.length == did.length && spname.length == syears.length) {
				Wrapper wrapper = Condition.create().like("pid", project.getId());
				iSubprojectService.delete(wrapper);
				List list = new ArrayList();
				for (int i = 0; i < spname.length; i++) {
					Subproject subproject = new Subproject();
					subproject.setName(spname[i]);
					subproject.setPid(project.getId());
					subproject.setDid(did[i]);
					subproject.setYears(new BigDecimal(syears[i]));
					subproject.setRemarks(sremarks[i]);
					list.add(subproject);
				}
				iSubprojectService.insertBatch(list);
			}
		}
		if (!CommonUtil.isBlank(pgeid) && !CommonUtil.isBlank(pgpid) && !CommonUtil.isBlank(pgtype)) {
			if (pgeid.length == pgpid.length && pgeid.length == pgtype.length) {
				Wrapper wrapper2 = Condition.create().eq("pid", pgpid[0]);
				iProjectgroupService.delete(wrapper2);
				List list2 = new ArrayList<>();
				for (int i = 0; i < pgeid.length; i++) {
					Projectgroup projectgroup = new Projectgroup();
					projectgroup.setEid(pgeid[i]);
					projectgroup.setPid(pgpid[i]);
					projectgroup.setType(new BigDecimal(pgtype[i]));
					projectgroup.setRemarks(pgremarks[i]);
					list2.add(projectgroup);
				}
				iProjectgroupService.insertBatch(list2);
			}
		}
		// 更新基础信息
		return this.updateById(project);
	}

	@Override
	public boolean insert(Project project, String[] spname, String[] did, String[] syears, String[] sremarks, String[] pgeid, String[] pgpid, String[] pgtype, String[] pgremarks) {

		if (!CommonUtil.isBlank(project)) {
			iProjectService.insert(project);
		}
		// TODO Auto-generated method stub
		if (!CommonUtil.isBlank(spname) && !CommonUtil.isBlank(did) && !CommonUtil.isBlank(syears)) {
			if (spname.length == did.length && spname.length == did.length && spname.length == syears.length) {
				List list = new ArrayList();
				for (int i = 0; i < spname.length; i++) {
					Subproject subproject = new Subproject();
					subproject.setName(spname[i]);
					subproject.setPid(project.getId());
					subproject.setDid(did[i]);
					subproject.setYears(new BigDecimal(syears[i]));
					subproject.setRemarks(sremarks[i]);

					list.add(subproject);
				}
				iSubprojectService.insertBatch(list);
			}
		}
		if (!CommonUtil.isBlank(pgeid) && !CommonUtil.isBlank(pgpid) && !CommonUtil.isBlank(pgtype)) {
			if (pgeid.length == pgpid.length && pgeid.length == pgtype.length) {
				Wrapper wrapper2 = Condition.create().eq("pid", pgpid[0]);
				iProjectgroupService.delete(wrapper2);
				List list2 = new ArrayList<>();
				for (int i = 0; i < pgeid.length; i++) {
					Projectgroup projectgroup = new Projectgroup();
					projectgroup.setEid(pgeid[i]);
					projectgroup.setPid(project.getId());
					projectgroup.setType(new BigDecimal(pgtype[i]));
					projectgroup.setRemarks(pgremarks[i]);
					list2.add(projectgroup);
				}
				iProjectgroupService.insertBatch(list2);
			}
		}
		return true;
	}

}
